package com.atguigu.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.constant.MessageConstant;
import com.atguigu.dao.MemberDao;
import com.atguigu.dao.OrderMobileDao;
import com.atguigu.dao.OrderSettingDao;
import com.atguigu.entity.Result;
import com.atguigu.pojo.Member;
import com.atguigu.pojo.Order;
import com.atguigu.pojo.OrderSetting;
import com.atguigu.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@Service(interfaceClass = OrderMobileService.class)
@Transactional
public class OrderMobileServiceImpl implements OrderMobileService {





//5、预约成功，更新当日的已预约人数


    @Autowired
    OrderMobileDao orderMobileDao;

    @Autowired
    OrderSettingDao orderSettingDao;

    @Autowired
    MemberDao memberDao;

    @Override
    public Result submit(Map map) throws Exception {
        //    1、检查用户所选择的预约日期是否已经提前进行了预约设置，如果没有设置则无法进行预约
        String orderDateStr = (String) map.get("orderDate");
        Date orderDate = DateUtils.parseString2Date(orderDateStr);
        OrderSetting orderSetting = orderSettingDao.getOrderSettingByOrderDate(orderDate);
        if(orderSetting==null){
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //2、检查用户所选择的预约日期是否已经约满，如果已经约满则无法预约
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if(number<=reservations){
            return new Result(false,MessageConstant.ORDER_FULL);
        }
        String idCard = (String) map.get("idCard");
        //4、检查当前用户是否为会员，如果是会员则直接完成预约，如果不是会员则自动完成注册并进行预约
        Member member = memberDao.getMemberByIdCard(idCard);
        if(member==null){
            String name = (String) map.get("name");
            String sex = (String) map.get("sex");
            String telphone = (String) map.get("telphone");
            member = new Member();
            member.setIdCard(idCard);
            member.setName(name);
            member.setSex(sex);
            member.setPhoneNumber(telphone);
            member.setRegTime(new Date());
            memberDao.add(member);
        }
        //3、检查用户是否重复预约（同一个用户在同一天预约了同一个套餐），如果是重复预约则无法完成再次预约
        Integer setmealId = Integer.parseInt(map.get("setmealId").toString());

        Order order = new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(orderDate);
        order.setSetmealId(setmealId);
        order.setOrderType(Order.ORDERTYPE_WEIXIN);
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        Integer count = orderMobileDao.getCountByOrder(order);
        if(count >= 1){
            return new Result(false,MessageConstant.HAS_ORDERED);
        }
        orderMobileDao.add(order);

        return new Result(true,MessageConstant.ORDER_SUCCESS,order);

    }

    @Override
    public Map findById(Integer id) {
        return orderMobileDao.findById(id);
    }
}
